package com.mk.controller.user;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mk.entity.Pet;
import com.mk.result.Result;
import com.mk.service.IPetService;
import com.mk.vo.PetNumberPlateVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 饲养证业务
 */
@RestController
@RequestMapping("/user/feedCertificate")
@RequiredArgsConstructor
public class FeedCertificateController {

    private final IPetService petService;

    /**
     * 饲养证登记
     * @param feedCertificate
     * @return
     */
    @CrossOrigin
    @PutMapping
    public Result registerFeedCertificate(String feedCertificate, Long id) {
        LambdaUpdateWrapper<Pet> wrapper = new LambdaUpdateWrapper<Pet>()
                .set(Pet::getFeedCertificate, feedCertificate)
                .set(Pet::getFeedStatus, Pet.NORMAL)
                .set(Pet::getFeedTime, LocalDateTime.now())
                .set(Pet::getUpdateTime, LocalDateTime.now())
                .eq(Pet::getId, id);
        petService.update(wrapper);
        return Result.success();
    }

    /**
     * 根据宠物id获取饲养证证明信息
     * @param id
     * @return
     */
    @CrossOrigin
    @GetMapping("/id")
    public Result<PetNumberPlateVO> getFeedCertificate(Long id) {
        Pet pet = petService.getById(id);
        PetNumberPlateVO petNumberPlateVO = new PetNumberPlateVO();
        petNumberPlateVO.setFeedCertificate(pet.getFeedCertificate());
        return Result.success(petNumberPlateVO);
    }

    /**
     * 根据id删除饲养证证明信息
     * @param id
     * @return
     */
    @CrossOrigin
    @DeleteMapping("/delete/{id}")
    public Result deleteFeedCertificate(@PathVariable Long id) {
        petService.update(new LambdaUpdateWrapper<Pet>()
                .set(Pet::getFeedCertificate, null)
                .set(Pet::getUpdateTime, LocalDateTime.now())
                .eq(Pet::getId, id));
        return Result.success();
    }
}
